<?php
/**
 +------------------------------------------------------------------------------
 * @author   heihei2202 <admin@sameus.com>
 * @version    2012-05-15 04:59:14Z heihei2202 $
 +------------------------------------------------------------------------------
 */
// 本类由系统自动生成，仅供测试用途
class GroupuserAction extends CenterAction {
    /*
     * 调取添加会员页面
     */
    //查看会员
    public function user_view(){
       $groupview=D('GroupsView');
          
        $data['uid']= $_SESSION['uid'];
        $data=$groupview->where($data)->field('gid,uid,user_name,user_email,user_say,user_info,state')->select();
        $this->assign('userlist',$data); 
        $this->display('Group:user_view');
    }
    public function audit_user(){
            $gid=$_GET['gid'];
            $group_user=M('Groupuser_audit');
            $auditlist=$group_user->where(array('group_id'=>$gid))->select();
            $this->assign('audituser',$auditlist);
            $this->display('Group:audit_user');
    }
  
    public function   audit_user_insert(){
         $group_user=M('Groups_user');
          $audit_user=M('Groupuser_audit');
         $data['group_id']=$_POST['gid'];
         $data['user_id']=$_POST['uid'];
         
         $redata['id']=$_POST['id'];
         $redata['refuse_reason']=$_POST['refuse_reason'];
         if($_POST['audit_state']==1){
             $redata['state']=2;
             $audit_user->save($redata);
             if($group_user->where($data)->find()){
                $this->success('已添加为会员');
             }else{
                 $group_user->add($data);
                 $this->success('审核会员通过');
             }
             
         }else{
              $redata['state']=0;
             if($audit_user->save($redata)){
                  $this->success('回复成功');
             }
             
         }
         
         //role
         
    } 
    //主动添加会员
      public function add_users(){
           $this->display('Group:add_user');
      }
      public function insert_users(){
          //先查询该邮箱是否存在.存在调取id,不存在插入新数据
          $email  = '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/';
        
          $_POST['email'] != null && preg_match ($email,$_POST['email']) ? $email=$_POST['email'] : $this->error('邮箱格式不对') ;
          $user=M('User');
          $group_user=M('Groupuser_audit');
          $guser=M('Groups_user');
            
          $datas['user_email']=$email;
          $udata=$user->where($datas)->field('id')->find(); 
       
          if($udata != false){
               $uid= $udata['id'];  
          }else{
              $uid=$user->add($datas); 
          }
          //查询是否已成为该群组会员,非会员则添加
          $data['group_id']=$_POST['gid'];
          $data['user_id']= $uid;
          if($data=$group_user->where($data)->find()){
            $this->success('您已经提交申请');
          }else{
              //$data['user_name']= $_POST['uname'];
              $data['refuse_reason']= $_POST['refuse_reason'];
              $data['create_time']=date('Y-m-d H:i:m',time()); 
              $data['state']=1;
              if($data=$group_user->add($data)){
                  //添加成功,则发送邀请邮件
                  $this->sendemail($email,$_POST['gname']);
                  //将该会员添加到群组会员关系表中
                  $gudata['group_id']=$_POST['gid'];
                  $gudata['user_id']=$uid;
                  $gudata['role']= $_POST['role'];
                 if(null != $guser->where($gudata)->find()){
                     $this->error('已添加') ;
                 }else{
                     //添加会员成功后,向该会员发送邮件
                     $guser->add($gudata);
                     $this->success('success') ;
                 }
              }else{
                  $this->error('error');
              }
          }
      }
    /*
     * 调取会员申请
     */
       public function groupuser_apply(){
         
          $group_user=M('Groupuser_audit');
          //apply_reason,group_id
          $data['group_id']=$_GET['gid'];
          $data['user_id']= $_SESSION['uid'];
          //$data['state']=1;
          //检测是否已经申请
          if($data=$group_user->where($data)->find()){
              
              $this->success('您已经提交申请');
              
          }else{
              $data['group_id']=$_GET['gid'];
              $data['user_id']= $_SESSION['uid'];
             // $data['apply_reason']=$_GET['apply_reason'];
              $data['user_name']= $_SESSION['uname'];
              $data['create_time']=date('Y-m-d H:i:m',time());
              $data['state']=1;
              if($data=$group_user->add($data)){
                  $this->success('success');
              }else{
                  $this->error('error');
              }
              
          }
    }
     
   
}